﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Maticsoft.DBUtility;
using Utils;
using System.Collections.Specialized;
using Maticsoft.Model;

public partial class shopcart_show : FBasePage
{
    // Fields
    public List<Car> carlist = new List<Car>();

    public int count = 0;
    public int farfee = 0;
    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public Maticsoft.BLL.orderdetail orderdebll = new Maticsoft.BLL.orderdetail();
    public orderdetail orderdemodel = new orderdetail();
    public Order ordermodel = new Order();
    public int pagecount = 0;
    public string pagenum = "1";
    public string strpage;
    public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
    public List<Team> teamlist = new List<Team>();
    public Team teammodel = new Team();

    // Methods
    public void AddCar()
    {
        if (!(this.Page.User.Identity.IsAuthenticated && !(this.Page.User.Identity.Name == "")))
        {
            base.Response.Redirect("/account/Login.aspx");
        }
        bool flag = false;
        decimal num = 0M;
        int num2 = 0;
        this.carlist = CookieCar.GetCarData();
        if (this.carlist != null)
        {
            foreach (Car car in this.carlist)
            {
                num += car.Price * car.Quantity;
                num2 += car.Quantity;
                if (car.Quantity < 0)
                {
                    flag = true;
                }
            }
            if (flag)
            {
                this.SetError("友情提示：请输入正确的数字");
            }
            else
            {
                this.ordermodel.User_id = base.AsUser.Id;
                this.ordermodel.State = "unpay";
                this.ordermodel.Quantity = num2;
                this.ordermodel.Create_time = DateTime.Now;
                if (base.CurrentCity != null)
                {
                    this.ordermodel.City_id = base.CurrentCity.Id;
                }
                else
                {
                    this.ordermodel.City_id = 0;
                }
                this.ordermodel.Fare = this.GetFarfee();
                this.ordermodel.Origin = num + this.ordermodel.Fare;
                this.ordermodel.Express = "Y";
                this.ordermodel.Ip_Address = CookieHelper.GetCookieValue("gourl");
                this.orderbll.Add(this.ordermodel);
                foreach (Car car in this.carlist)
                {
                    this.orderdemodel.Num = new int?(car.Quantity);
                    this.orderdemodel.Teamid = new int?(Convert.ToInt32(car.Qid));
                    this.orderdemodel.Teamprice = new decimal?(car.Price);
                    this.orderdemodel.Order_id = new int?(this.orderbll.GetMaxId());
                    this.orderdebll.Add(this.orderdemodel);
                }
                base.Response.Redirect("confirmation.aspx?orderid=" + this.orderbll.GetMaxId());
            }
        }
    }

    public string GetBBsPageStr(int count, int pagenum, int acinum)
    {
        StringBuilder builder = new StringBuilder();
        int num = count / acinum;
        int num2 = count % acinum;
        if (num2 > 0)
        {
            num++;
        }
        builder.Append("<UL class=paginator>");
        for (int i = 1; i <= num; i++)
        {
            if (i == pagenum)
            {
                builder.Append(string.Concat(new object[] { "<LI class=current><a href='show.aspx?pgnum=", i, "'><font>", i, "</font></a></li>" }));
            }
            else
            {
                builder.Append(string.Concat(new object[] { "<LI><a href='show.aspx?pgnum=", i, "'>", i, "</a></li>" }));
            }
        }
        builder.Append("<a href='#'>>></a>");
        builder.Append("</ul>");
        return builder.ToString();
    }

    public decimal GetFarfee()
    {
        decimal num = 0M;
        int num2 = 0;
        try
        {
            this.carlist = CookieCar.GetCarData();
            decimal[] m = new decimal[this.carlist.Count];
            for (int i = 0; i < this.carlist.Count; i++)
            {
                m[i] = decimal.Parse(this.carlist[i].Fee);
                num2 += Convert.ToInt32(this.carlist[i].Quantity);
            }
            m = Utility.sort(m);
            num = m[0];
            this.farfee = Convert.ToInt32(CookieCar.GetProid(num.ToString()));
            if (num2 >= this.farfee)
            {
                return 0M;
            }
            num = m[0];
        }
        catch (Exception)
        {
        }
        return num;
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        base.SetTitle("购物车");
        if (base.Request["pgnum"] != null)
        {
            if (Utility.IsNum(base.Request["pgnum"].ToString()))
            {
                this.pagenum = base.Request["pgnum"].ToString();
            }
            else
            {
                this.SetError("您输入的参数非法");
            }
        }
        this.Search();
        this.SearchTodayTeam();
        if (base.Request["buy2"] == "确认无误，去结算")
        {
            this.AddCar();
        }
    }

    public void Search()
    {
        this.carlist = CookieCar.GetCarData();
        this.GetFarfee();
    }

    public void SearchTodayTeam()
    {
        int num;
        if (((base.ASSystem.Guowushu == 0) || (base.ASSystem.Guowushu.ToString() == "")) || (base.ASSystem.Guowushu.ToString() == null))
        {
            this.pagecount = 30;
        }
        else
        {
            this.pagecount = base.ASSystem.Guowushu;
        }
        this.teamlist = this.teambll.SearchTodayTeam(this.pagecount, Convert.ToInt32(this.pagenum), "*", "sort_order desc,id asc", this.sqlWhere, out num);
        if (this.teamlist.Count == 0)
        {
            this.strpage = "对不起，没有相关数据";
        }
        else if (num >= this.pagecount)
        {
            this.strpage = this.GetBBsPageStr(num, Convert.ToInt32(this.pagenum), this.pagecount);
        }
    }

    // Properties
    public int pages
    {
        get
        {
            if (this.ViewState["pages"] != null)
            {
                return Convert.ToInt32(this.ViewState["pages"].ToString());
            }
            return 1;
        }
        set
        {
            this.ViewState["pages"] = value;
        }
    }

    public string sqlWhere
    {
        get
        {
            if (this.ViewState["sqlWhere"] != null)
            {
                return this.ViewState["sqlWhere"].ToString();
            }
            return (" Begin_time<='" + DateTime.Now.ToString() + "' and End_time>='" + DateTime.Now.ToString() + "' and ((Delivery!='coupon' and Farefree!=0 and Card<=0 ) or (Max_number != 0 and Now_number > Max_number)) and Team_type='normal'");
        }
        set
        {
            this.ViewState["sqlWhere"] = value;
        }
    }

}